package com.wzt.lianfirecontrol.utils;

import android.content.Context;

import com.wzt.lianfirecontrol.bean.ConstData;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SimpleCache {

    /**
     * 添加到本地保存一天
     * @param activity
     * @param key
     * @param value
     * @return
     */
    public static boolean saveDataForOneDay(Context activity, String key, String value) {
        if (StringUtils.isEmpty(value)) {
            return false;
        }

        String oldValue = PreferencesUtils.getString(activity, key, "");
        String todayStr = TimeUtils.getCurrentTime("yyyy-MM-dd");
        if (!StringUtils.isEmpty(oldValue)) {
            if (oldValue.startsWith(todayStr)) {
                if (oldValue.contains(value)) {
                    return false;
                }
                PreferencesUtils.putString(activity, key, oldValue + ";" + value);
                return true;
            }
        }
        PreferencesUtils.putString(activity, key, todayStr + ";" + value);
        return true;
    }

    /**
     * 删除本地保存一天的key数据
     * @param activity
     * @param key
     * @return
     */
    public static void clearDataForOneDay(Context activity, String key) {
        PreferencesUtils.putString(activity, key, "");
    }

    /**
     * 是否包含
     * @param activity
     * @param key
     * @param value
     * @return
     */
    public static boolean isContainValueForOneDay(Context activity, String key, String value) {
        String oldValue = PreferencesUtils.getString(activity, key, "");
        if (!StringUtils.isEmpty(oldValue) && !StringUtils.isEmpty(value)) {
            String todayStr = TimeUtils.getCurrentTime("yyyy-MM-dd");
            return oldValue.startsWith(todayStr) && oldValue.contains(value);
        }
        return false;
    }

    /**
     * 保存数据到本地，计时单位为小时
     * @param context
     * @param key
     * @param value
     * @param hours
     */
    public static void saveDataForHours(Context context, String key, String value, String hours){
        if (StringUtils.isEmpty(value)){
            return;
        }
        if (isContainValueForHours(context, key, value, hours)){
            return;
        }
        PreferencesUtils.putString(context, key, TimeUtils.getCurrentTime(ConstData.TIME_FORMAT_YYYYMMDDHHMMSS) + ";" + value);
    }

    /**
     * 是否包含data
     * @param context
     * @param key
     * @param value
     * @param hours
     * @return
     */
    public static boolean isContainValueForHours(Context context, String key, String value, String hours){
        String oldValue = PreferencesUtils.getString(context, key);
        if (!StringUtils.isEmpty(oldValue) && !StringUtils.isEmpty(value)){
            String[] strings = oldValue.split(";");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ConstData.TIME_FORMAT_YYYYMMDDHHMMSS);
            try {
                Date date = simpleDateFormat.parse(strings[0]);
                if (StringUtils.isEmpty(hours)){
                    hours = "24";
                }
                long hour = Long.parseLong(hours);
                if (hour > TimeUtils.getDifferenceHours(date) && oldValue.contains(value)){
                    return true;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

}